import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class LoadingInterceptors extends InterceptorsWrapper {
  int _requestCount = 0;

  @override
  Future onRequest(RequestOptions options) async {
    _requestCount += 1;
    if (_requestCount == 1) {
      EasyLoading.show(status: '加载中');
    }
    return super.onRequest(options);
  }

  @override
  Future onResponse(Response response) async {
    _requestCount -= 1;
    if (_requestCount == 0) {
      EasyLoading.dismiss();
    }
    return super.onResponse(response);
  }

  @override
  Future onError(DioError err) {
    _requestCount -= 1;
    if (_requestCount == 0) {
      EasyLoading.dismiss();
    }
    print("ERROR[${err?.response?.statusCode}] => PATH: ${err?.request?.path}");
    return super.onError(err);
  }
}
